﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinForm_Bankapp
{
    public class ATM
    {
        public event EventHandler<BigMoneyArgs> BigMoneyFetched;

        private Random random = new Random();

        public void WithdrawMoney(Account account, decimal amount)
        {
            try
            {
                if (random.Next(1, 11) <= 3)
                {
                    throw new BadCashException("Bad cash detected.");
                }

                account.Withdraw(amount);

                if (amount > 10000)
                {
                    OnBigMoneyFetched(account, amount);
                }
            }
            catch (InsufficientBalanceException ex)
            {
                Console.WriteLine($"Insufficient balance: {ex.Message}");
                MessageBox.Show($"Insufficient balance: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (BadCashException ex)
            {
                Console.WriteLine($"Bad cash detected: {ex.Message}");
                MessageBox.Show($"Bad cash detected: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        protected virtual void OnBigMoneyFetched(Account account, decimal amount)
        {
            BigMoneyArgs args = new BigMoneyArgs(account, amount);
            BigMoneyFetched?.Invoke(this, args);
        }
    }
}
